/*
    Copyright 2011 - Tommi Laukkanen (www.substanceofcode.com)

    This file is part of DoodleDrive.

    DoodleDrive is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DoodleDrive is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with DoodleDrive. If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0

Rectangle {
    id: window
    width: 640
    height: 360

    Item {
        id: game
        property int score: 0
        property int highscore: 0
        property int level: 0
    }

    Image {
        height: parent.height
        source: "pics/paper.png"
    }

    Image {
        y: -40
        x: parent.width - width - 20
        source: "pics/sun.png"
        PropertyAnimation on rotation { to: 360; duration: 40000; loops: Animation.Infinite }
    }

    Image {
        y: 20
        x: parent.width
        source: "pics/cloud.png"
        PropertyAnimation on x { id: cloud1anim; from: window.width; to: -width - 20; duration: 18000; loops: Animation.Infinite }
    }

    Image {
        y: 50
        x: parent.width + 200
        source: "pics/cloud.png"
        PropertyAnimation on x { id: cloud2anim; from: window.width + 200; to: -width - 20; duration: 15800; loops: Animation.Infinite }
    }

    Image {
        y: parent.height - height - 30
        x: parent.width + 400
        source: "pics/tree.png"
        PropertyAnimation on x { id: treeanim; from: window.width + 400; to: -width - 1400; duration: 16000; loops: Animation.Infinite }
    }

    Image {
        id: rock1
        y: parent.height - height
        x: parent.width + 1100
        source: "pics/rock_small.png"
        PropertyAnimation on x { id: rock1anim; from: window.width + 1400 ; to: -width - 700; duration: 15000 - game.level; loops: Animation.Infinite }
    }

    Image {
        id: rock2
        y: parent.height - height
        x: parent.width + 1940
        source: "pics/rock_group.png"
        PropertyAnimation on x { id: rock2anim; from: window.width + 1940; to: -width - 260; duration: 16000 - game.level; loops: Animation.Infinite }
    }

    function collision() {
        console.log("Collided");
        car.state = "broken";
        rock1anim.pause();
        rock2anim.pause();
        treeanim.pause();
        cloud1anim.pause();
        cloud2anim.pause();
        tire1anim.pause();
        tire2anim.pause();
        groundanim.pause();
        collisionAnimation.pause();
        console.log("Animations paused");

        if(game.highscore<game.score) {
            game.highscore = game.score;
        }

    }

    function restart() {
        console.log("Restarting")
        homeText.visible = false;
        car.state = "idle";
        game.score = 0;
        game.level = 0;

        rock1anim.resume();
        rock2anim.resume();
        treeanim.resume();
        cloud1anim.resume();
        cloud2anim.resume();
        tire1anim.resume();
        tire2anim.resume();
        groundanim.resume();

        rock1anim.restart();
        rock2anim.restart();
        treeanim.restart();
        cloud1anim.restart();
        cloud2anim.restart();
        tire1anim.restart();
        tire2anim.restart();
        groundanim.restart();
        collisionAnimation.resume();
        collisionAnimation.restart();
    }

    Text {
        x: 40
        y: 2
        font.pixelSize: 16
        text: "HIGH SCORE: " + game.highscore
    }
    Text {
        x: 40
        y: 18
        font.pixelSize: 16
        text: "SCORE: " + game.score
    }

    Item {
        id: car
        x: 50
        y: parent.height - height - 30
        width: 150
        height: 109
        state: "gameover"

        Component.onCompleted: {
            rock1anim.pause();
            rock2anim.pause();
            treeanim.pause();
            cloud1anim.pause();
            cloud2anim.pause();
            tire1anim.pause();
            tire2anim.pause();
            groundanim.pause();
            collisionAnimation.pause();

            //collisionAnimation.start();
        }

        SequentialAnimation {
            id: collisionAnimation
            loops: Animation.Infinite
            PauseAnimation { duration: 100 }
            ScriptAction {
                script: {
                    game.score += 8; // Magic number #1
                    if(game.level<12000) {
                        game.level += 6; // Magic number #2
                    }
                    // Collision detection for rock 1
                    if( rock1.x<car.x+120 &&
                            rock1.x>car.x &&
                            car.y>window.height - car.height - 40) {
                        // Collision
                        collision();
                    }
                    // Collision detection for rock 2
                    if( rock2.x<car.x+150 &&
                            rock2.x>car.x &&
                            car.y>window.height - car.height - 40) {
                        // Collision
                        collision();
                    }
                }
            }
        }

        Image {
            source: "pics/car2.png"
        }

        Image {
            y: 30
            x: - 10
            source: "pics/tire1.png"
            PropertyAnimation on rotation { from: 0; id: tire1anim; to: 360; duration: 1000; loops: Animation.Infinite }
        }

        Image {
            y: 30
            x: 80
            source: "pics/tire2.png"
            PropertyAnimation on rotation { from: 0; id: tire2anim; to: 360; duration: 1000; loops: Animation.Infinite }
        }

        states: [
            State {
                name: "jump"
            },
            State {
                name: "broken"
                changes: [
                    PropertyChanges {
                        target: car
                        rotation: 160
                    },
                    PropertyChanges {
                        target: car
                        y: car.y
                    }
                ]
            }

        ]

        transitions: [
            Transition {
                to: "jump"
                SequentialAnimation {
                    ParallelAnimation {
                        id: jumpAnimation
                        NumberAnimation { target: car; properties: "y"; to: car.y - car.height; easing.type: Easing.OutCirc; duration: 600 }
                        NumberAnimation { target: car; properties: "rotation"; to: -20; easing.type: Easing.OutCirc; duration: 600 }
                    }
                    ParallelAnimation {
                        id: landingAnimation
                         NumberAnimation { target: car; properties: "y"; to: window.height - car.height - 30; easing.type: Easing.OutBounce; duration: 1600 }
                         NumberAnimation { target: car; properties: "rotation"; to: 0; easing.type: Easing.OutQuad; duration: 1100 }
                    }
                    ScriptAction {
                        script: car.state = "idle";
                    }
                }
            },
            Transition {
                id: brokenTransition
                to: "broken"
                SequentialAnimation {
                    NumberAnimation { id: brokenAnimation; target: car; properties: "y,rotation"; easing.type: Easing.OutQuad; duration: 500 }
                    PauseAnimation { id: brokenWaitAnimation; duration: 2000 }
                    ScriptAction {
                        script: {
                            car.state = "gameover";
                            homeText.visible = true;
                        }
                    }
                }
            }
        ]
    }

    Image {
        id: ground
        x: 0
        y: parent.height - 50
        width: 2400
        source: "pics/ground.png"
        PropertyAnimation on x { from: 0; id: groundanim; to: -1600; duration: 5000; loops: Animation.Infinite }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: {
            if(car.y > window.height - car.height - 40 && car.state!="broken" && car.state!="gameover") {
                landingAnimation.complete();
                car.state = "idle";
                car.state = "jump";
            } else if(car.state=="gameover"
                      && !brokenAnimation.running
                      && !brokenWaitAnimation.running) {
                restart();
            }
        }
    }

    Image {
        source: "pics/delete.png"
        x: 5
        y: 5
        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
    }

    Text {
        id: homeText
        anchors.horizontalCenter: parent.horizontalCenter
        y: 64
        text: "<center><b>DoodleDrive v1.0</b><br/>Created by Tommi & Topias Laukkanen<br/>www.substanceofcode.com<br/><br/>Tap the screen to start.<br/> Tap to jump!</center>"
        font.pixelSize: 22
    }

}
